<script setup lang="ts">
import { reqSkusList, reqDeleteAllSkus, reqAddSkus, reqUpdateSkus, reqUpdateSkusStatus } from '@/api/skus'
import { useInitTable, useInitForm } from '@/composables/useCommon'
const {
  tableData,
  loading,
  getData,
  handleDelete,
  handleStatusChange,
  currentPage,
  pageSize,
  total,
  tableRef,
  handleDeleteAll,
  handleSelectionChange
} = useInitTable({
  getList: reqSkusList,
  delete: reqDeleteAllSkus,
  updateStatus: reqUpdateSkusStatus
})

const { formDrawerRef, formRef, drawerTitle, form, rules, handleSubmit, handleAdd, handleEdit } = useInitForm({
  getData,
  initialForm: { name: '', default: '', status: 1, order: 50 },
  rules: {
    name: [{ required: true, message: '请输入规格名称', trigger: 'blur' }]
  },
  update: reqUpdateSkus,
  create: reqAddSkus
})
</script>

<template>
  <div>
    <el-card shadow="never" style="border: 0; margin: 10px">
      <ListHeader layout="create,refresh,delete" @refresh="getData" @create="handleAdd" @delete="handleDeleteAll" />
      <el-table
        ref="tableRef"
        v-loading="loading"
        :data="tableData"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column prop="name" label="规格名称" />
        <el-table-column prop="default" label="规格值" />
        <el-table-column prop="order" label="排序" width="100" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-switch
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="0"
              @change="handleStatusChange(scope.row, scope.row.status)"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300">
          <template #default="scope">
            <el-button type="primary" text size="small" icon="edit" @click="handleEdit(scope.row)">编辑</el-button>
            <el-popconfirm title="是否确认要删除吗?" width @confirm="handleDelete(scope.row.id)">
              <template #reference>
                <el-button size="small" text type="danger" icon="delete">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="prev, pager, next, total"
          :current-page="currentPage"
          :page-size="pageSize"
          :total="total"
          @current-change="getData"
        ></el-pagination>
      </div>
    </el-card>

    <FormDrawer ref="formDrawerRef" :title="drawerTitle" @submit="handleSubmit" destroyOnClose>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="规格名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入规格名称" style="width: 30%"></el-input>
        </el-form-item>
        <el-form-item label="规格值" prop="default">
          <TagInput v-model="form.default" />
        </el-form-item>
        <el-form-item label="排序" prop="order">
          <el-input-number v-model="form.order" :min="0" :max="999" style="width: 30%"></el-input-number>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="0" />
        </el-form-item>
      </el-form>
    </FormDrawer>
  </div>
</template>

<style lang="scss" scoped>
.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 10px;
}
</style>
